#include "Chord.h"
#include <EEPROM.h>
#include "HardwareSerial.h"
#include <MIDI.h>

Chord::Chord(MIDI *midi) {
  this->midi = midi;
  notec = 0;
}

void Chord::readFromSerial() {
  if (Serial.available() > 0) {
    notec = Serial.read();
    delayMicroseconds(4250);
    for (int i = 0; i < notec; i++) {
      notes[i] = Serial.read();
      delayMicroseconds(4250);
    }
  }  
}

void Chord::writeToSerial() {
  Serial.write(notec);
  for (int i = 0; i < notec; i++) {
    Serial.write(notes[i]);  
  }
}

void Chord::readFromMemory(int index) {
  notec = EEPROM.read(index);
  for (int i = 1; i <= notec; i++) {
    notes[i - 1] = EEPROM.read(index + i);  
  }
}

void Chord::writeToMemory(int index) {
  EEPROM.write(index, notec);
  for (int i = 1; i <= notec; i++) {
    EEPROM.write(index + i, notes[i - 1]);  
  }
}

void Chord::handleNoteOnOff() {
  midi->send(midi->getStatus(), midi->getData1(), midi->getData2());
  for (int i = 0; i < notec; i++) {
    midi->send(midi->getStatus(), midi->getData1() + notes[i], midi->getData2());
  }
}
